home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
-
- typedef unsigned long DWORD;
- typedef unsigned short WORD;
-
- /* this header structure comes from a wav play/record program by
- Andre Fuechsel (af1@irz.inf.tu-dresden.de), but he used code from
- Liam Corner, so this might be his too. */
-
- typedef struct { /* header for WAV-Files */
- char main_chunk[4]; /* 'RIFF' */
- DWORD length; /* length of file */
- char chunk_type[4]; /* 'WAVE' */
- char sub_chunk[4]; /* 'fmt' */
- DWORD length_chunk; /* length sub_chunk, always 16 bytes */
- WORD format; /* always 1 = PCM-Code */
- WORD modus; /* 1 = Mono, 2 = Stereo */
- DWORD sample_fq; /* Sample Freq */
- DWORD byte_p_sec; /* Data per sec */
- WORD byte_p_spl; /* bytes per sample, 1=8 bit, 2=16 bit (mono)
- 2=8 bit, 4=16 bit (stereo) */
- WORD bit_p_spl; /* bits per sample, 8, 12, 16 */
- char data_chunk[4]; /* 'data' */
- DWORD data_length; /* length of data */
- } wave_header;
-
- FILE *in= 0, *out= 0;
- char *buf;
- char infile[80], outfile[80];
- int data_size, tmp, i;
- wave_header head;
-
- main(int argc, char **argv) {
- if (argc> 1) {
- strcpy(infile, argv[1]);
- in= fopen(infile, "rb");
- if (!in) printf("Unable to open %s\n", infile);
- else printf("Input: %s\n",infile);
- }
- while (!in) {
- printf("Input file: ");
- fgets(infile, 80, stdin);
- infile[strlen(infile)-1]= 0;
- if (!infile[0]) exit(1);
- in= fopen(infile, "rb");
- if (!in) printf("Unable to open %s\n", infile);
- }
- if (argc> 2) strcpy(outfile, argv[2]);
- else {
- for (i=strlen(infile);infile[i]!='.' && i; i--) ;
- if (i) infile[i]= 0;
- strcpy(outfile, infile);
- strcat(outfile, ".wav");
- }
- out= fopen(outfile, "wb");
- if (!out) { printf("Unable to open %s\n", outfile); exit(1); }
- printf("Output: %s\n",outfile);
-
- fseek(in, 0, SEEK_END);
- buf = (char*) malloc(data_size= ftell(in));
- fseek(in, 0, SEEK_SET);
- fread(buf, 1, data_size, in);
- fclose(in);
-
- strncpy(head.main_chunk, "RIFF", 4);
- strncpy(head.chunk_type, "WAVE", 4);
- strncpy(head.sub_chunk, "fmt ", 4);
- strncpy(head.data_chunk, "data", 4);
- head.length_chunk = 16;
- head.format = 1;
- printf("# of channels: ");
- scanf("%d",& head.modus);
- printf("Sample Rate: ");
- scanf("%u", &head.sample_fq);
- printf("Bits Per Sample: ");
- scanf("%u",& tmp);
- head.bit_p_spl = tmp;
- head.byte_p_spl = (tmp >> 3)* head.modus;
- head.byte_p_sec = head.sample_fq * head.byte_p_spl;
- head.data_length = data_size;
- head.length = head.data_length + 36;
- if (fwrite(&head, 1, sizeof(head), out) != sizeof(head)) {
- printf("error writing header\n");
- exit(1);
- }
- fwrite(buf, 1, data_size, out);
- fclose(out);
- }
-
-